package com.example.aaaapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.aaaapp.R;
import com.example.aaaapp.api.Api;
import com.example.aaaapp.api.ApiConfig;
import com.example.aaaapp.api.TtitCallBack;
import com.example.aaaapp.entity.BaseRespModel;
import com.example.aaaapp.utils.KeyboardUtils;
import com.example.aaaapp.utils.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import okhttp3.OkHttpClient;

public class LoginActivity extends BaseActivity {

    Context mContext;
    private Button btnLogin;
    private EditText etAccount;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;
        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                login(account, pwd);
            }
        });

    }

    void login(String account, String pwd) {
//        if (StringUtils.isEmpyt(account)) {
//            ShowUtils.showMessage(this, "账号不能为空");
//            return;
//        }
//        if (StringUtils.isEmpyt(pwd)) {
//            ShowUtils.showMessage(this, "密码不能为空");
//            return;
//        }
        account = "123";
        pwd = "345";
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        HashMap<String, Object> p = new HashMap<>();
        p.put("account",account);
        p.put("pwd",pwd);

        Api.config(ApiConfig.LoginUrl, p).postRequest(this, new TtitCallBack() {
            @Override
            public void onSuccess(String res) {
                Gson gson = new Gson();
                BaseRespModel respModel = gson.fromJson(res.toString(), BaseRespModel.class);

                if (StringUtils.SuccessCode.equals(respModel.getCode())) {
                    saveStringToSp("token", respModel.getResult().getToken());
                    navigationTo(HomeActivity.class);
                    showMessageAsync(res);
                } else {
                    showMessageAsync(respModel.getMessage());
                }
            }

            @Override
            public void onFailure(Exception e) {
                showMessageAsync(e.getMessage());
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        etAccount.endBatchEdit();
        etPwd.endBatchEdit();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            KeyboardUtils.hideKeyboard(ev, view, this);
        }
        return super.dispatchTouchEvent(ev);
    }
}